package com.oss.service;

import cn.hutool.core.lang.Validator;
import com.jfinal.kit.Prop;
import com.oss.model.Area;
import com.oss.model.Dealer;
import com.oss.model.Employee;
import com.oss.model.Series;
import com.oss.utils.constants.DataDictionary;
import com.oss.view.InvokeResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DealerService {
    //经销商DAO
    private static final Dealer dealerDao = new Dealer().dao();
    //地区树DAO
    private static final Area areaDao = new Area().dao();
    //系列DAO
    private static final Series seriesDao = new Series().dao();
    private static final Employee employeeDao = new Employee().dao();


    // 获取配置文件
    private static Prop prop = DataDictionary.getProp();


//    /**
//     * 批量补充门店名称
//     */
//    public InvokeResult updateName() {
//        List<Dealer> list = dealerDao.find("SELECT * FROM dealer WHERE `name` IS NULL");
//        if (list.size() > 0) {
//            for (Dealer dealer : list) {
//                Integer cityId = dealer.getCity();
//                Area city = areaDao.findById(cityId);
//                dealer.setName(city.getName() + "门店");
//                dealer.update();
//            }
//        }
//        return InvokeResult.success();
//    }

    /**
     * 根据经销商获取旗下的系列列表
     */
    public InvokeResult getSeriesListByDealer(String employeeId) {
        Employee employee = employeeDao.findById(employeeId);
        if (employee == null) {
            return InvokeResult.failure("该账号已失效");
        }

        Dealer dealer = dealerDao.findById(employee.getDealerId());
        if (dealer == null) {
            return InvokeResult.failure("该经销商不存在");
        }

        List<Object> list = new ArrayList<>();
        String seriesIds = dealer.getSeriesId();
        if (Validator.isNotEmpty(seriesIds)) {
            String[] split = seriesIds.split(",");
            if (split.length > 0) {
                for (String seriesId : split) {
                    Series series = seriesDao.findById(seriesId);
                    if (Validator.isNotEmpty(series)) {
                        Map<String, Object> item = new HashMap<>();
                        item.put("id", seriesId);
                        item.put("name", series.getName());
                        list.add(item);
                    }
                }
            }
        }
        return InvokeResult.success(list);
    }


    /**
     * 根据lv返回列表：0=中国,1=省,2=市,3=区
     *
     * @param lv
     * @return
     */
    public InvokeResult findListByLv(Integer lv, Integer pid) {
        return InvokeResult.success(areaDao.findListByLv(lv, pid));
    }


    /**
     * 获取门店的省份列表
     *
     * @return InvokeResult
     */
    public InvokeResult getProvinceList() {
        //lv：0=中国,1=省,2=市,3=区
        List<Area> provinceList = areaDao.find("SELECT a.id,a.name FROM AREA a,dealer d WHERE a.id = d.province AND a.lv=1 GROUP BY a.id ORDER BY a.id ASC ");
        return InvokeResult.success(provinceList);
    }

    /**
     * 获取门店的区份列表
     *
     * @param provinceId provinceId
     * @return InvokeResult
     */
    public InvokeResult getCityList(Integer provinceId) {
        //lv：0=中国,1=省,2=市,3=区
        List<Area> provinceList = areaDao.find("SELECT a.id,a.name FROM AREA a,dealer d WHERE a.id = d.city AND a.pid=? AND a.lv=2 GROUP BY a.id ORDER BY a.id ASC ", provinceId);
        return InvokeResult.success(provinceList);
    }

    /**
     * 根据cityId获取门店列表
     *
     * @param cityId cityId
     * @return
     */
    public InvokeResult getDealerList(Integer cityId) {
        List<Dealer> dealerList = dealerDao.find("SELECT * FROM dealer WHERE city=?", cityId);
        return InvokeResult.success(dealerList);
    }


    /**
     * 门店的系列列表
     */
    public InvokeResult getSeriesList() {
        List<Series> seriesList = seriesDao.find("SELECT * FROM series");
        return InvokeResult.success(seriesList);
    }


    /**
     * 根据系列找门店列表
     *
     * @param seriesId
     * @return
     */
    public InvokeResult getDealerListBySeries(Integer seriesId) {
        List<Dealer> dealerList = dealerDao.find("SELECT * FROM dealer WHERE series_id=?", seriesId);
        return InvokeResult.success(dealerList);
    }

}
